package org.linlinjava.litemall.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.linlinjava.litemall.admin.beans.Constants;
import org.linlinjava.litemall.admin.controller.address.vo.AdminAddressPageReqVO;
import org.linlinjava.litemall.admin.controller.address.vo.AdminAddressPageRespVO;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallAddressService;
import org.linlinjava.litemall.db.service.LitemallRegionService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.util.BeanUtil;
import org.linlinjava.litemall.db.util.CollectionUtils;
import org.linlinjava.litemall.db.util.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.Optional;

@Service
public class AdminAddressService {

    @Autowired
    private LitemallAddressService addressService;

    @Autowired
    private LitemallUserService userService;

    @Autowired
    private LitemallRegionService regionService;

    public IPage<AdminAddressPageRespVO> page(AdminAddressPageReqVO pageReqVO) {
        LitemallRegion region = regionService.getById(pageReqVO.getRegionId());

        LambdaQueryWrapper<LitemallAddress> lambdaQueryWrapper = new LambdaQueryWrapper();
        Optional.ofNullable(region).ifPresent(r -> {
            if (Constants.ADMINISTRATION_REGION_PROVINCE.equals(r.getType())) {
                lambdaQueryWrapper.eq(LitemallAddress::getProvince, r.getNameEn());
            } else if (Constants.ADMINISTRATION_REGION_CITY.equals(r.getType())) {
                lambdaQueryWrapper.eq(LitemallAddress::getCity, r.getNameEn());
            } else {
                lambdaQueryWrapper.eq(LitemallAddress::getCountry, r.getNameEn());
            }
        });
        Optional.ofNullable(pageReqVO.getName())
                .filter(StringUtils::isNotEmpty)
                .ifPresent(name -> lambdaQueryWrapper.like(LitemallAddress::getName, name));
        Page<LitemallAddress> page = addressService.page(new Page<>(pageReqVO.getPage(), pageReqVO.getLimit()),lambdaQueryWrapper);
        Map<Integer, LitemallUser> userMap = userService.getUserMap(CollectionUtils.convertList(page.getRecords(), LitemallAddress::getUserId));

        Page<AdminAddressPageRespVO> adminAddressPageRespPage = BeanUtil.copyPage(page, AdminAddressPageRespVO.class);
        adminAddressPageRespPage.getRecords().forEach(item->{
            item.setUser(new AdminAddressPageRespVO.UserVO());
            MapUtils.findAndThen(userMap,item.getUserId(),user ->{
                item.setUser(BeanUtil.copy(user,AdminAddressPageRespVO.UserVO.class));
            });
        });
        return adminAddressPageRespPage;
    }
}
